<template>
  <div class="container">
    <!--    头部筛选-->
    <div class="hd">
      <el-form :model="searchForm" inline ref="searchRef">
        <el-form-item prop="name">
          <el-input
            size="small"
            v-model="searchForm.name"
            placeholder="请输入包房名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="canteen">
          <el-select
            v-model="searchForm.canteen"
            class="m-2"
            placeholder="请选择食堂"
            size="small"
          >
            <el-option
              v-for="item in canteenOptions"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="audit">
          <el-select
            v-model="searchForm.audit"
            class="m-2"
            placeholder="请选择审核状态"
            size="small"
          >
            <el-option
              v-for="item in auditOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <TsButton
            perms="managerAudit:auditList:get"
            label="搜索"
            size="small"
            type="success"
            @click="search"
          ></TsButton>
          <el-button size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--    表格区域-->
    <div
      class="table"
      style="height: calc(100% - 160px); margin-top: 20px; overflow-y: auto"
    >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#FEF6F6' }"
      >
        <el-table-column prop="canteenName" label="食堂名称" width="200">
        </el-table-column>
        <el-table-column prop="messageName" label="包房名称" width="200">
        </el-table-column>
        <el-table-column prop="orderEatTime" label="预定时间" width="200">
        </el-table-column>
        <el-table-column prop="orderEatType" label="用餐类型" width="200">
          <template #default="scope">
            <span v-show="scope.row.orderEatType === '0'">午餐</span>
            <span v-show="scope.row.orderEatType === '1'">晚餐</span>
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="预订人" width="200">
        </el-table-column>
        <el-table-column prop="price" label="用餐标准" width="200">
        </el-table-column>
        <el-table-column prop="orderStatus" label="目前状态" width="100">
          <template #default="scope">
            <el-tag
              type="danger"
              size="small"
              v-show="scope.row.orderStatus === '0'"
              >未审核</el-tag
            >
            <el-tag
              type="success"
              size="small"
              v-show="scope.row.orderStatus === '1'"
              >已审核</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="240px">
          <template #default="scope">
            <TsButton
              perms="managerAudit:auditDetail:$id:get"
              style="color: #3cb6e6"
              type="text"
              size="mini"
              label="详情"
              @click="details(scope.row.orderId)"
            ></TsButton>
            <TsButton
              perms="managerAudit:$orderId:admin:delete"
              style="color: #f30606"
              type="text"
              size="mini"
              label="撤销"
              @click="del(scope.row.orderId)"
            ></TsButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { canteenList, roomList, cancelOrder } from "@/api/canteen/roomAudit";
import commonPageRepository from "@/composables/common-page-repository";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const searchForm = ref({});
//获取食堂数据
const canteenOptions = ref([]);
function getCanteenList() {
  canteenList().then((res) => {
    canteenOptions.value = res.data;
  });
}
//审核状态数据
const auditOptions = ref([
  {
    name: "未审核",
    id: "0",
  },
  {
    name: "已审核",
    id: "1",
  },
]);
//表格数据
const tableData = ref([]);
//分页查询数据
const pageNum = ref(1);
const pageSize = ref(100);
const totalNum = ref(0);
//获取分页数据
function getRoomList() {
  roomList({
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getRoomList
);
//搜索
function search() {
  roomList({
    messageName: searchForm.value.name,
    canteenName: searchForm.value.canteen,
    orderStatus: searchForm.value.audit,
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
const searchRef = ref(null);
//重置
function reset() {
  searchRef.value.resetFields();
  getRoomList();
}
const router = useRouter();
//跳转到详情
function details(id) {
  router.push({
    name: "roomAuditDetail",
    query: {
      id,
    },
  });
}
//撤销审核
function del(id) {
  ElMessageBox.confirm("您确定要撤销这条审核数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      cancelOrder(id).then((res) => {
        if (res.code === 200) {
          ElMessage.success("撤销成功");
          getRoomList();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
onMounted(() => {
  getCanteenList();
  getRoomList();
});
</script>

<style scoped lang="scss">
.container {
  height: calc(100vh - 200px);
  background: #ffffff;
  box-shadow: 0 0 16px rgba(96, 96, 96, 0.16);
  border-radius: 6px;
  padding: 20px;
  overflow-y: auto;
  margin-top: 20px;
}
</style>
